﻿using System;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Entities;
using PowerToolsWeb.Facade;

namespace PowerToolsWeb.Admin
{
    public partial class AddAdmin : AdminPageBase
    {
        private bool IsAdd
        {
            get { return (Request.QueryString["adminid"] == null); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (IsAdd)
                {
                    this.spanTitle.InnerText = "添加管理员";
                    this.btnChangePwd.Visible = false;
                    this.btnAdd.Visible = true;
                }
                else
                {
                    this.spanTitle.InnerText = "修改密码";
                    this.btnChangePwd.Visible = true;
                    this.btnAdd.Visible = false;
                    int adminid = 0;
                    if (!int.TryParse(Request.QueryString["adminid"].ToString(), out adminid))
                    {
                        Response.Redirect("AdminList.aspx");
                    }
                    Tpt_Admin admin = new Tpt_Admin();
                    if (!admin.SelectByPk(adminid))
                    {
                        Response.Redirect("AdminList.aspx");
                    }
                    this.txbLoginName.Text = admin.LOGINNAME;
                    this.txbLoginName.Enabled = false;
                }
            }
        }

        protected void btnChangePwd_Click(object sender, EventArgs e)
        {
            SystemUser sy = (SystemUser)Session["SystemUser"];
            int adminid = 0;
            if (!int.TryParse(Request.QueryString["adminid"].ToString(), out adminid))
            {
                Response.Redirect("AdminList.aspx");
            }
            Tpt_Admin admin = new Tpt_Admin();
            if (!admin.SelectByPk(adminid))
            {
                Response.Redirect("AdminList.aspx");
            }
            if (UserInfo.UserName != "admin" && UserInfo.UserName != admin.LOGINNAME)
            {
                Alert(Page, "不是超级管理员，不能修改别人的密码", "AdminList.aspx");
                return;
            }
            if (this.txbPwd.Text == "" || this.txbPwd.Text.Trim().Length < 6)
            {
                Alert(Page, "密码不能为空并且必须是不少于6位的数字或字母");
                return;
            }
            if (this.txbPwd.Text.Trim() != this.txbRePwd.Text.Trim())
            {
                Alert(Page, "两次输入密码不一致");
                return;
            }
            admin.PWD = txbPwd.Text.Trim();
            if (!admin.Update())
            {
                Alert(Page, "修改密码失败");
                return;
            }
            Alert(Page, "修改密码成功", "AdminList.aspx");
        }

        protected void btnCencle_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminList.aspx");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txbLoginName.Text == "")
            {
                Alert(Page, "登录名不能为空");
                return;
            }
            if (this.txbPwd.Text == "" || this.txbPwd.Text.Trim().Length < 6)
            {
                Alert(Page, "密码不能为空并且必须是不少于6位的数字或字母");
                return;
            }
            if (this.txbPwd.Text.Trim() != this.txbRePwd.Text.Trim())
            {
                Alert(Page, "两次输入密码不一致");
                return;
            }
            Tpt_Admin admin = new Tpt_Admin();
            if (admin.GetPwdByUserName(txbLoginName.Text.Trim()))
            {
                Alert(Page, "此登录名的管理员已存在");
                return;
            }
            admin.LOGINNAME = txbLoginName.Text.Trim();
            admin.PWD = txbPwd.Text.Trim();
            if (!admin.Insert())
            {
                Alert(Page, "添加管理员失败");
                return;
            }
            Alert(Page, "添加管理员成功", "AdminList.aspx");
        }
    }
}
